package com.itheima.my_reggie.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.my_reggie.common.Result;
import com.itheima.my_reggie.dto.DishDto;
import com.itheima.my_reggie.dto.SetmealDto;
import com.itheima.my_reggie.pojo.Category;
import com.itheima.my_reggie.pojo.Dish;
import com.itheima.my_reggie.pojo.Setmeal;
import com.itheima.my_reggie.service.ICategoryService;
import com.itheima.my_reggie.service.IDishFlavorService;
import com.itheima.my_reggie.service.IDishService;
import com.itheima.my_reggie.service.ISetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private ISetmealService setmealService;

    @Autowired
    private ICategoryService categoryService;


//    分页查询功能
    @GetMapping("/page")
    public Result<Page> setmealPage(Integer page,Integer pageSize,String name){
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        setmealService.page(setmealPage,setmealWrapper);
        Page<SetmealDto> setmealDtoPage = new Page<>();
        BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");
        List<SetmealDto> setmealDtosList = setmealPage.getRecords().stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Category category = categoryService.getById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(setmealDtosList);
        return Result.success(setmealDtoPage);
    }

//    新增套餐信息
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping
    public Result<String> SetmealSave(@RequestBody SetmealDto setmealDto){
        setmealService.saveWithDish(setmealDto);
        return Result.success("套餐添加成功");
    }

//    查询单个套餐数据，用于套餐的更新
    @GetMapping("/{id}")
    public Result<SetmealDto> getByIdSetmeal(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getByIdWithDish(id);
        return Result.success(setmealDto);
    }

//    更新套餐信息
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PutMapping
    public Result<String> SetmealUpdate(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return Result.success("套餐添加成功");
    }

    //    更新套餐状态
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping("/status/{status}")
    public Result<String> SetmealhUpdateByIds(@PathVariable Integer status,@RequestParam List<Long> ids){
        LambdaUpdateWrapper<Setmeal> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Setmeal::getStatus,status);
        updateWrapper.in(Setmeal::getId,ids);
        setmealService.update(updateWrapper);
        return Result.success("套餐修改成功");
    }

//    删除套餐信息
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping
    public Result removeSetmeal(@RequestParam List<Long> ids){
        setmealService.removeWithDish(ids);
        return Result.success("套餐删除成功");
    }

    @Cacheable(value = "setmealCache",key = "'setmeal-'+#categoryId",unless = "#result == null")
    @GetMapping("/list")
    public Result<List<Setmeal>> list(Long categoryId){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId,categoryId);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);
        return Result.success(list);
    }
}
